using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

using TTDotNet.Engine.ThreeDEngine;

namespace TTDotNet.Engine.GUI
{
    public class EditBox : Control
    {
        private const int Backspace = (int)System.Windows.Forms.Keys.Back;
        private const int Delete = (int)System.Windows.Forms.Keys.Delete;
        private const int Left = (int)System.Windows.Forms.Keys.Left;
        private const int Right = (int)System.Windows.Forms.Keys.Right;
        private const int End = (int)System.Windows.Forms.Keys.End;
        private const int Home = (int)System.Windows.Forms.Keys.Home;
        private const int Return = (int)System.Windows.Forms.Keys.Return;

        private int _cursorPosition;
        private int _maxLength;
        private bool flash;
        private Stopwatch sw;


        public EditBox(int id, Rectangle screenRect, string text, float fontSize, int maxLength, Color textColor, string fontName,
                        GameTexture bg)
        {
            _id = id;
            _size = screenRect;
            _size.Width = bg.Texture.GetLevelDescription(0).Width;
            _size.Height = bg.Texture.GetLevelDescription(0).Height;
            _text = text;
            _fontSize = fontSize;
            _fontColor = textColor;
            _position = new Point(screenRect.X, screenRect.Y);
            textureUp = bg;
            _cursorPosition = text.Length;
            _maxLength = maxLength;

            _textRect = screenRect;

            System.Drawing.Font systemFont = new System.Drawing.Font(fontName, fontSize);
            _font = new Microsoft.DirectX.Direct3D.Font(Game.device, systemFont);

            SetSize(screenRect.Width, screenRect.Height);

            sw = new Stopwatch();
            sw.Start();
        }

        public override void Render(PointF location)
        {
            Game.guiSprite.Transform = spriteTransform;
            Game.guiSprite.Draw(textureUp.Texture, Rectangle.Empty, Vector3.Empty, _renderPosition, Color.White);

            string temp = _text;

            if (_hasFocus)
            {
                if (flash)
                {
                    temp = _text.Insert(_cursorPosition, "_");
                }
                else
                {
                    temp = _text.Insert(_cursorPosition, "  ");
                }
            }

            if (sw.ElapsedMilliseconds > 400) { flash = !flash; sw.Reset(); sw.Start(); }

            //_font.DrawText(null, temp, new Rectangle((int)_textRect.X, (int)_textRect.Y, (int)_textRect.Width, (int)_textRect.Height), DrawTextFormat.Left | DrawTextFormat.VerticalCenter, _fontColor);
        }

        protected override void OnMouseRelease(Point cursor)
        {
        }

        public override bool OnKeyDown(List<Keys> pressedKeys, char pressedChar, int pressedKey)
        {
            if(!_hasFocus) 
            {
                return false;
            }

            if (pressedKey == Backspace)
            {
                if (_cursorPosition > 0)
                {
                    _cursorPosition--;
                    _text = _text.Remove(_cursorPosition, 1);
                    return true;
                }
            }
            else if(pressedKey == Delete)
            {
                if (_cursorPosition < _text.Length)
                {
                    _text = _text.Remove(_cursorPosition, 1);
                    return true;
                } 
            }
            else if (pressedKey == Left)
            {
                if (_cursorPosition <= 0) return true;

                _cursorPosition--;
                return true;
            }
            else if (pressedKey == Right)
            {
                if (_cursorPosition >= _text.Length) return true;

                _cursorPosition++;
                return true;
            }
            else if (pressedKey == End)
            {
                _cursorPosition = _text.Length;

            }
            else if (pressedKey == Home)
            {
                _cursorPosition = 0;
                return true;
            }

            if (char.IsLetterOrDigit(pressedChar) || char.IsPunctuation(pressedChar) || char.IsSymbol(pressedChar) || char.IsWhiteSpace(pressedChar))
            {
                //System.Windows.Forms.MessageBox.Show("Got in deep");

                if (_text.Length < _maxLength && pressedChar != char.MinValue)
                {
                    if (_cursorPosition == _text.Length)
                    {
                        _text += pressedChar.ToString();
                    }
                    else
                    {
                        _text = _text.Insert(_cursorPosition, pressedChar.ToString());
                    }
                    _cursorPosition++;
                }
            }

            return true;
        }

        public void ReleaseFocus()
        {
            if (_hasFocus)
            {
                _hasFocus = false;
            }
        }

        public override ControlState State
        {
            get
            {
                return base.State;
            }
            set
            {
                base.State = value;
                if (Disabled)
                {
                    ReleaseFocus();
                }
            }
        }

    }
}
